<template>
	<div class="container" align="center">
		<el-form v-show="showSearch" ref="queryForm" :model="query" :inline="true" label-width="68px">
			<el-form-item label="参数名称" prop="configName">
				<el-input v-model="query.configName" placeholder="请输入参数名称" clearable size="small" style="width: 240px"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="参数键名" prop="configKey">
				<el-input v-model="query.configKey" placeholder="请输入参数键名" clearable size="small" style="width: 240px"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="系统内置" prop="configType">
				<el-select v-model="query.configType" placeholder="系统内置" clearable size="small">
					<el-option v-for="dict in mmap" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>

			</el-form-item>
			<el-form-item label="创建时间">
				<el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd"
					type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>

		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
					@click="handleUpdate">修改
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete">删除
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-refresh" size="mini" @click="handleRefreshCache">刷新缓存
				</el-button>
			</el-col>
		</el-row>

		<el-table v-loading="loading" :data="configList" stripe @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="参数主键" align="center" prop="configId" />
			<el-table-column label="参数名称" align="center" prop="configName" :show-overflow-tooltip="true" />
			<el-table-column label="参数键名" align="center" prop="configKey"  :show-overflow-tooltip="true" />
			<el-table-column label="参数键值" align="center" prop="configValue" />
			<el-table-column label="系统内置" align="center" prop="configType" />
			<el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
			<el-table-column label="创建时间" align="center" prop="createTime" width="180" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改
					</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 画分页条 -->
		<div v-show="total>0" class="pagination-container">
			<el-pagination :current-page="query.page" :page-sizes="[10, 20, 30, 50,100]"
				layout="total, sizes, prev, pager, next, jumper" :page-size="query.size" :total="total"
				@size-change="handleSizeChange" @current-change="handleCurrentChange" />
		</div>

		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="参数名称" prop="configName">
					<el-input v-model="form.configName" placeholder="请输入参数名称" />
				</el-form-item>
				<el-form-item label="参数键名" prop="configKey">
					<el-input v-model="form.configKey" placeholder="请输入参数键名" />
				</el-form-item>
				<el-form-item label="参数键值" prop="configValue">
					<el-input v-model="form.configValue" placeholder="请输入参数键值" />
				</el-form-item>

				<el-form-item label="参数状态" prop="configType">
					<el-radio v-model="form.configType" label="Y">是</el-radio>
					<el-radio v-model="form.configType" label="N">否</el-radio>
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listConfig,
		getConfig,
		delConfig,
		addConfig,
		updateConfig
	} from '../../../api/config.js'

	export default {
		name: 'Config',
		data() {
			return {
				loading: true,
				dateRange: [],
				configList: [],
				total: 0,
				form: {},
				title: '',
				open: false,
				ids: [],
				single: true,
				multiple: true,
				showSearch: true,

				query: {
					page: 1,
					size: 10,
					configName: undefined,
					configKey: undefined,
					configType: undefined,
					configValue: undefined,
					createTime: undefined
				},
				rules: {
					configName: [{
						required: true,
						message: '参数名称不能为空',
						trigger: 'blur'
					}],
					configkey: [{
						required: true,
						message: '参数键名不能为空',
						trigger: 'blur'
					}],
					configValue: [{
						required: true,
						message: '参数键值不能为空',
						trigger: 'blur'
					}]
				},
				mmap: [{
					label: '正常',
					value: 'Y'
				}, {
					label: '停用',
					value: 'N'
				}]
			}
		},
		created() {
			this.getList()
		},
		methods: {
			handleAdd() {
				console.log('dddddddddddddd')
				this.reset()
				this.open = true
				this.title = '添加参数'
			},
			getList() {
				this.loading = true
				listConfig(this.addDateRange(this.query, this.dateRange)).then(response => {
					console.log(response)
					this.configList = response.data
					console.log('woshi')
					
					console.log('woshi')
					this.total = response.count
					this.loading = false
				})
			},
			cancel() {
				this.open = false
				this.reset()
			},
			handleQuery() {
				this.query.page = 1
				this.getList()
			},

			resetQuery() {
				this.dateRange = []
				this.resetForm('queryForm')
				this.handleQuery()
			},
			// 重置
			reset() {
				this.form = {
					configId: undefined,
					configName: undefined,
					configkey: undefined,
					configValue: undefined,
					configType: '',
					remark: undefined
				}
				this.resetForm('')
			},
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.configId)
				// eslint-disable-next-line eqeqeq
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			// 当每页显示多少发生变化
			handleSizeChange(val) {
				this.query.size = val
				// 查询数据
				this.getList()
			},
			// 点击了第几页
			handleCurrentChange(val) {
				this.query.page = val
				// 查询数据
				this.getList()
			},

			handleUpdate(row) {
				console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
				console.log(row)
				console.log(this.ids)
				this.reset()
				const configId = row.configId || this.ids

				getConfig(configId).then(response => {
					console.log('ddddddddddddddddddddddddddddd')
					console.log(response.data)
					this.form = response.data
					this.open = true
					this.title = '修改参数'
				})
			},
			handleDelete(row) {
				console.log('ssssssssssss')
				console.log(this.ids)
				console.log('ssssssssssss')

				const configIds = row.configId || this.ids
				console.log('ssssssssssss')
				console.log(configIds)
				console.log('ssssssssssss')
				this.$modal.confirm('是否确认删除参数编号为"' + configIds + '"的数据项？').then(function() {
					//console.log(configId)
					return delConfig(configIds)

				}).then(() => {
					this.getList()
					this.$modal.msgSuccess('删除成功')
				}).catch(() => {})

			},

			handleRefreshCache() {
				// eslint-disable-next-line no-undef
				refreshCache().then(() => {
					this.$modal.msgSuccess('刷新成功')
				})
			},
			handleExport() {
				this.download('config/export', {
					...this.query
				}, `config_${new Date().getTime()}.xlsx`)
			},
			submitForm: function() {
				this.$refs['form'].validate(valid => {
					console.log('ddddddddddddddddddd')
					console.log(this.$refs['form'])
					if (valid) {
						// eslint-disable-next-line eqeqeq
						if (this.form.configId != undefined) {
							console.log('zzzzzzzzzz')
							console.log(this.form)
							updateConfig(this.form).then(response => {
								this.$modal.msgSuccess('修改成功')
								this.open = false
								this.getList()
							})
						} else {
							addConfig(this.form).then(response => {
								console.log('qqqqqqqqqqqqqqq')
								console.log(response)
								this.$modal.msgSuccess('新增成功')

								this.open = false
								this.getList()
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
